﻿using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SelectCardUI : MonoBehaviour
{
    [SerializeField] private Animator animator;
    public float animationDuration = 1.0f;
    public GameObject content;
    public GameObject cardPrefab;
    
    public CardLibrarySO cardLibrary;
    public CardLibrarySO selectedCardLibrary;
    private List<CardUI> cardList = new List<CardUI>();
    
    public Image buildingChessBgImage,buildingChessImage;
    public TMP_Text buildingHpCount,buildingSoliderCount;
    
    public Image soliderChessBgImage1,soliderChessImage1;
    public TMP_Text soliderHpCount1,soliderAttackCount1;
    
    public Image soliderChessBgImage2,soliderChessImage2;
    public TMP_Text soliderHpCount2,soliderAttackCount2;
    
    public Text buildingName,buildingSoliderName,soliderName1,soliderName2;
    public Text buildingSkill,soliderSkill1,soliderSkill2;
    
    public GameObject soliderGO1,soliderGO2,parent;
    private void OnEnable()
    {
        EventCenter.GetInstance().AddEventListener("UpdateSelectedCard",UpdateSelectedCard);
        EventCenter.GetInstance().AddEventListener<BuildingChessDateSO>("ShowCardDetail",ShowCardDetail);
        selectedCardLibrary.cardLibraryList.Clear();
        cardList.Clear();
        foreach (var date in cardLibrary.cardLibraryList)
        {
            var go = Instantiate(cardPrefab, content.transform);
            go.GetComponent<CardUI>().InitCard(date);
            cardList.Add(go.GetComponent<CardUI>());
        }
    }

    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener("UpdateSelectedCard",UpdateSelectedCard);
        EventCenter.GetInstance().RemoveEventListener<BuildingChessDateSO>("ShowCardDetail",ShowCardDetail);
    }

    private void ShowCardDetail(BuildingChessDateSO date)
    {
        parent.SetActive(true);
        
        buildingChessBgImage.sprite = date.chessBackgroundSprite;
        buildingChessImage.sprite = date.chessSprite;
        buildingHpCount.text = date.health.ToString();
        buildingSoliderCount.text = date.soldierChessDates.Count.ToString();
        buildingName.text = date.chessName;

        if (date.skill.Count != 0)
        {
            buildingSkill.text = "技能: " + date.description;
        }
        else
        {
            buildingSkill.text = "技能: 无";
        }
        
        if (date.soldierChessDates.Count == 0)
        {
            buildingSoliderName.gameObject.SetActive(false);
            
            soliderGO1.gameObject.SetActive(false);
            soliderGO2.gameObject.SetActive(false);
        }
        else if (date.soldierChessDates.Count == 1)
        {
            
            soliderGO1.gameObject.SetActive(true);
            soliderGO2.gameObject.SetActive(false);
            
            buildingSoliderName.gameObject.SetActive(true);
            buildingSoliderName.text = "可召唤兵种 "+date.soldierChessDates[0].chessName;
            
            SoldierChessDateSO soldierChessDate1 = date.soldierChessDates[0];
            soliderName1.text = soldierChessDate1.chessName;
            soliderChessBgImage1.sprite = soldierChessDate1.chessBackgroundSprite;
            soliderChessImage1.sprite = soldierChessDate1.chessSprite;
            soliderHpCount1.text = soldierChessDate1.health.ToString();
            soliderAttackCount1.text = soldierChessDate1.attack.ToString();
            soliderName1.text = soldierChessDate1.chessName;

            if (soldierChessDate1.skill.Count != 0)
            {
                soliderSkill1.text = "技能: " + soldierChessDate1.description;
            }
            else
            {
                soliderSkill1.text = "技能: 无";
            }
        }
        else if (date.soldierChessDates.Count == 2)
        {
            
            soliderGO1.gameObject.SetActive(true);
            soliderGO2.gameObject.SetActive(true);
            
            buildingSoliderName.gameObject.SetActive(true);
            buildingSoliderName.text = "可召唤兵种 "+date.soldierChessDates[0].chessName + "  "  + date.soldierChessDates[1].chessName;
            
            SoldierChessDateSO soldierChessDate1 = date.soldierChessDates[0];
            soliderName1.text = soldierChessDate1.chessName;
            soliderChessBgImage1.sprite = soldierChessDate1.chessBackgroundSprite;
            soliderChessImage1.sprite = soldierChessDate1.chessSprite;
            soliderHpCount1.text = soldierChessDate1.health.ToString();
            soliderAttackCount1.text = soldierChessDate1.attack.ToString();

            if (soldierChessDate1.skill.Count != 0)
            {
                soliderSkill1.text = "技能: " + soldierChessDate1.description;
            }
            else
            {
                soliderSkill1.text = "技能: 无";
            }
            
            SoldierChessDateSO soldierChessDate2 = date.soldierChessDates[1];
            soliderName2.text = soldierChessDate2.chessName;
            soliderChessBgImage2.sprite = soldierChessDate2.chessBackgroundSprite;
            soliderChessImage2.sprite = soldierChessDate2.chessSprite;
            soliderHpCount2.text = soldierChessDate2.health.ToString();
            soliderAttackCount2.text = soldierChessDate2.attack.ToString();

            if (soldierChessDate2.skill.Count != 0)
            {
                soliderSkill2.text = "技能: " + soldierChessDate2.description;
            }
            else
            {
                soliderSkill2.text = "技能: 无";
            }
        }
    }

    private void UpdateSelectedCard()
    {
        foreach (var cardUI in cardList)
        {
            cardUI.UpdateSelectedCard();
        }
    }

    public void  loadBattleScene()
    {
        if(selectedCardLibrary.cardLibraryList.Count > 0)
        {
            StartCoroutine(LoadSceneAfterAnimation());
        }
    }
    private IEnumerator LoadSceneAfterAnimation()
    {
        if (animator != null)
        {
            // 触发渐入动画
            animator.SetBool("FadeIn", true);

            // 等待动画播放完毕
            yield return new WaitForSeconds(animationDuration);
        }
        else
        {
            Debug.LogError("Animator 未赋值！");
        }
        
        // 获取当前活跃场景
        Scene currentActiveScene = SceneManager.GetActiveScene();
        AsyncOperation ao = SceneManager.LoadSceneAsync("Battle", LoadSceneMode.Additive); // 使用 Additive 模式加载新场景
        Scene newScene = SceneManager.GetSceneByName("Battle");
        yield return ao;
        GameManager.GetInstance().StartGame(PatientCount.Instance.patientIndex + 1);
        // 设置新加载的场景为活跃场景
        SceneManager.SetActiveScene(newScene);
        // 卸载原来的活跃场景
        SceneManager.UnloadSceneAsync(currentActiveScene);
    }
}
